package cdc.retrofit.official.jsonqueryparameters;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.RequestBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

public class JsonStringConverterFactory extends Converter.Factory {
	private final Converter.Factory delegateFactory;

	JsonStringConverterFactory(Converter.Factory delegateFactory) {
		this.delegateFactory = delegateFactory;
	}

	@Override
	public Converter<?, String> stringConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
		for (Annotation annotation : annotations) {
			if (annotation instanceof Json) {
				// NOTE: If you also have a JSON converter factory installed in addition to this
				// factory,
				// you can call retrofit.requestBodyConverter(type, annotations) instead of
				// having a
				// reference to it explicitly as a field.
				Converter<?, RequestBody> delegate = delegateFactory.requestBodyConverter(type, annotations,
						new Annotation[0], retrofit);
				return new DelegateToStringConverter<>(delegate);
			}
		}
		return null;
	}
}
